package com.dhcc.sdc.busi.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.cdc.busi.dao.CdcEpisodeMapper;
import com.dhcc.cdc.busi.entity.CdcEpisodeEntity;
import com.dhcc.cdc.sys.dao.DictionaryMapper;
import com.dhcc.cdc.sys.entity.DictionaryEntity;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.annotion.SdcBusiLog;
import com.dhcc.sdc.annotion.SdcBusiType;
import com.dhcc.sdc.busi.dao.SdcBusiQcEliminateMapper;
import com.dhcc.sdc.busi.dao.SdcBusiQcTriggeredMapper;
import com.dhcc.sdc.busi.dao.SdcQcMapper;
import com.dhcc.sdc.busi.entity.SdcBusiQcEliminateEntity;
import com.dhcc.sdc.busi.entity.SdcBusiQcTriggeredEntity;
import com.dhcc.sdc.busi.entity.SdcQcEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("${sys.apiPath}/sdc/eliminate")
public class SdcBusiQcEliminateController extends BaseController {
    private String episodeId;
    private Long qcId;
    private String docId;
    private String qcDesc;
    private String PREFIX = "/backend/sdc/eliminate/";

	@Autowired
	private SdcBusiQcEliminateMapper sdcBusiQcEliminateMapper;
	
    @Autowired
    private DictionaryMapper dictionaryMapper;

    @Autowired
    private SdcQcMapper sdcQcMapper;

    @Autowired
    private SdcBusiQcTriggeredMapper sdcBusiQcTriggeredMapper;

    @Autowired
    private CdcEpisodeMapper cdcEpisodeMapper;

    @RequestMapping(value = "/add")
    @ResponseBody
    @SdcBusiLog(sdcBusiType = SdcBusiType.QC_ELIMINATE)
    public Object add(SdcBusiQcEliminateEntity sdcBusiQcEliminateEntity) {
        if (CommonUtil.isOneEmpty(sdcBusiQcEliminateEntity)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }

        //通过患者xcode查询患者id
        Wrapper<CdcEpisodeEntity> wrapper2 = new EntityWrapper<>();
        wrapper2.eq("xcode",sdcBusiQcEliminateEntity.getXcode());
        List<CdcEpisodeEntity> cdcEpisodeEntities = cdcEpisodeMapper.selectList(wrapper2);
        Long id = cdcEpisodeEntities.get(0).getId();

        sdcBusiQcEliminateEntity.setEpisodeId(id);
        this.sdcBusiQcEliminateMapper.insert(sdcBusiQcEliminateEntity);

        Wrapper<SdcBusiQcTriggeredEntity> wrapper3 = new EntityWrapper<>();
        wrapper3.eq("qc_id",qcId).eq("episode_id",id);
        sdcBusiQcTriggeredMapper.delete(wrapper3);
        return R.ok();
    }


    @RequestMapping(value = "/toAdd")
    public String toAdd(@RequestParam Map<String,Object> params, Model model) {
        episodeId = params.get("episodeId").toString();
        docId = params.get("docId").toString();
        qcDesc = params.get("qcDesc").toString();

        //通过qcDesc获取质控项的id
        Wrapper<SdcQcEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("qc_desc",qcDesc);
        List<SdcQcEntity> sdcQcEntities = sdcQcMapper.selectList(wrapper);
        qcId = sdcQcEntities.get(0).getId();

        //从字典中获取手动纠正原因
        Wrapper<DictionaryEntity> wrapper2 = new EntityWrapper<>();
        wrapper2.eq("dictype_id",1673907240799793154L);
        List<DictionaryEntity> list = dictionaryMapper.selectList(wrapper2);
        Map<Long, String> map = new HashMap<>();
        for (DictionaryEntity dictionaryEntity : list) {
            map.put(dictionaryEntity.getId(), dictionaryEntity.getDescription());
        }

        model.addAttribute("episodeId",episodeId);
        model.addAttribute("qcId",qcId);
        model.addAttribute("docId",docId);
        model.addAttribute("map", map);
        return PREFIX + "eliminate.html";
    }

}
